package com.example.myfiveapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

    private java.util.List<Goods> goodsList = new java.util.ArrayList<Goods>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initGoods(); // 初始化数据
        GoodsAdapter adapter = new GoodsAdapter(MainActivity.this, R.layout.fruit_item, goodsList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Goods goods = goodsList.get(position);
                String data = "排名："+goods.getIndex()+",名称："+goods.getName()+",搜索指数："+goods.getNumber();
                Intent intent = new Intent(MainActivity.this, ItemDetailActivity.class);
                intent.putExtra("data",data);
                startActivity(intent);
            }
        });
    }

    private void initGoods() {
        for (int i = 0; i < 4; i++) {
            Goods a = new Goods("1", "联想","1232212");
            goodsList.add(a);
            Goods b = new Goods("2", "戴尔","212412");
            goodsList.add(b);
            Goods c = new Goods("3", "华硕","122312");
            goodsList.add(c);
            Goods d = new Goods("4", "苹果","212312");
            goodsList.add(d);
            Goods e = new Goods("5", "惠普","12312");
            goodsList.add(e);
            Goods f = new Goods("6", "宏基","21212");
            goodsList.add(f);
            Goods g = new Goods("7", "索尼","12312");
            goodsList.add(g);
            Goods h = new Goods("8", "三星","21212");
            goodsList.add(h);
            Goods k = new Goods("9", "神舟","12312");
            goodsList.add(k);
        }
    }

}